﻿using FlowDesigner.Flow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace FlowDesigner.HelperVision
{
    public class VisuaXml
    {
        /// <summary>
        /// 根据 Canvas 的坐标点获取NodeS控件。
        /// </summary>
        /// <param name="canvas">Canvas 容器。</param>
        /// <param name="x">X 坐标。</param>
        /// <param name="y">Y 坐标。</param>
        /// <returns>在指定坐标点的控件，如果没有找到则返回 null。</returns>
        public static T GetElementAt<T>(Canvas canvas, double x, double y)where T : class
        {
            foreach (var element in canvas.Children)
            {
                var node = element as NodeS;
                if(node == null) continue;
                double left = Canvas.GetLeft(node);
                double top = Canvas.GetTop(node);
                double width = node.RenderSize.Width;
                double height = node.RenderSize.Height;

                if (x >= left && x <= left + width &&y >= top && y <= top + height)
                {
                    return element as T;
                }
            }

            return null;
        }
        /// <summary>
        /// 根据 Canvas 的坐标点获取Line控件。
        /// </summary>
        /// <param name="canvas">Canvas 容器。</param>
        /// <param name="x">X 坐标。</param>
        /// <param name="y">Y 坐标。</param>
        /// <returns>在指定坐标点的控件，如果没有找到则返回 null。</returns>
        public static List<object> GetElementAllLineornodes(Canvas canvas, double x, double y)
        {
            List<object> ListAll = new List<object>();
            foreach (var element in canvas.Children)
            {
                var line = element as Line;
                var node = element as NodeS;
                if (node == null&&line==null) continue;
                if (node != null)
                {
                    double left = Canvas.GetLeft(node);
                    double top = Canvas.GetTop(node);
                    double width = node.RenderSize.Width;
                    double height = node.RenderSize.Height;
                    if (x >= left && x <= left + width && y >= top && y <= top + height)
                    {
                        ListAll.Add(node);
                    }
                }
                else
                {
                    if (line.X1==x&&line.Y1==y)
                    {
                        ListAll.Add(line);
                    }
                }             
            }
            return ListAll;
        }
        /// <summary>
        /// 获取命中视感元素
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static NodeS FindParentNodeS(DependencyObject element)
        {
            while (element != null && !(element is NodeS))
            {
                element = VisualTreeHelper.GetParent(element);
            }
            return element as NodeS;
        }

        /// <summary>
        /// 检查指定位置是否有控件
        /// </summary>
        /// <param name="canvas">Canvas 对象</param>
        /// <param name="point">指定位置的点（相对于 Canvas）</param>
        /// <returns>控件（如果有）或 null</returns>
        public static NodeS GetElementAtPoint(Canvas canvas, Point point)
        {
            HitTestResult hitTestResult = VisualTreeHelper.HitTest(canvas, point);

            if (hitTestResult != null && hitTestResult.VisualHit is NodeS element)
            {
                return element; // 返回找到的控件
            }

            return null; // 如果没有找到控件，返回 null
        }
        /// <summary>
        /// 获取父容器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="child"></param>
        /// <returns></returns>
        public static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            // get parent item
            DependencyObject parentObject = LogicalTreeHelper.GetParent(child);

            // we've reached the end of the tree
            if (parentObject == null) return null;

            // check if the parent matches the type we're looking for
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // use recursion to proceed with next level
                return FindVisualParent<T>(parentObject);
            }
        }
        /// <summary>
        /// 获取指定子集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
        {
            // 遍历所有子元素
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);

                // 如果类型匹配，并且名称也匹配（如果提供了），则添加到结果列表中
                if (child != null && child is T)
                {
                    // 这里处理找到的子元素
                    // 例如: 输出找到的子元素
                   return (T)child;
                }

                // 递归查找
              return FindChild<T>(child);
            }
            return null;
        }
        /// <summary>
        /// 获取所有子集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dependencyObject"></param>
        /// <param name="chidrenlist"></param>
        /// <returns></returns>
        public static List<T> FindLogicalChildren<T>(DependencyObject dependencyObject, List<T> chidrenlist) where T : DependencyObject
        {
            // 遍历所有子元素
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);

                // 如果类型匹配，并且名称也匹配（如果提供了），则添加到结果列表中
                if (child != null && child is T)
                {
                    // 这里处理找到的子元素
                    // 例如: 输出找到的子元素
                    chidrenlist.Add((T)child);
                }

                // 递归查找
                FindLogicalChildren<T>(child, chidrenlist);
            }
            return chidrenlist;
        }
    }
}
